//
//  MyBankCarListVC.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/9/7.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import SVProgressHUD

class MyBankCarListVC: BaseViewController {
    
    var dataModels : [BankCardInfoModel]?
    
    weak var lastV : GetMoneyController?
    
    
    lazy var bottomView : OrderBottomView = {
        
        let temp : OrderBottomView = Bundle.main.loadNibNamed(self.StringFromClass(classType: OrderBottomView.self), owner: nil, options: nil)?.first as! OrderBottomView
        
        temp.frame = CGRect(x: 0, y: 0, width: KScreenW, height: 90)
        
        temp.btn.setTitle("添加银行卡", for: .normal)
        
        temp.isHidden = true
        
        temp.delegate = self
        
        return temp
        
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        
        navigationItem.title = "选择卡片类型"

        plainTableView.tableFooterView = bottomView
        
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "确定", style: .plain, target: self, action: #selector(dissMissYou))
        
        plainTableView.register(UINib(nibName: StringFromClass(classType: MyBankCarListCell.self), bundle: nil), forCellReuseIdentifier: MyBankCarListCell.identifer())
        
        weak var weakSelf = self
        
        plainTableView.mj_header =  ZMRefreshHeader.init {
            
            weakSelf!.loadData()
            
        }
        
        plainTableView.mj_header.beginRefreshing()
        
    }
    
    
    func dissMissYou(){
        
        for item in dataModels!{
        
            if item.isSelect == true {
                
                lastV?.model = item

            }
        }
        
        lastV?.groupTableView.reloadData()
        
        
        navigationController?.popViewController(animated: true)
        
    
    }
    
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        SVProgressHUD.dismiss()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }
    

   
}



extension MyBankCarListVC : OrderBottomViewDeleage{
    
    func orderBottomViewBtnDidTap(){
        
        let tmep = AddBankCardVC()
        
        tmep.block = {() -> () in
        
            self.loadData()
        
        }
        
        navigationController?.pushViewController(tmep, animated: true)
    
    }

}



extension MyBankCarListVC{

    
    func loadData() {
        
        NetworkTools.shared.sendRequest(method: .GET_METHOD, pathStr: "mobile/index.php?c=member_bank_card&a=card_list", parameters: nil) { [weak self] (responseObject, error)  in
            
            
            if error == nil {
                
                self?.dataModels = [BankCardInfoModel]()
                for dic in responseObject!["datas"]!["card_list"] as! [[String : AnyObject]]{
                    
                    self!.dataModels!.append(BankCardInfoModel(dic: dic))
                    
                }
                
                self?.bottomView.isHidden = false
                
                self!.plainTableView.mj_header.endRefreshing()
                
                self!.plainTableView.reloadData()
                
                
            }else{
                
                self!.plainTableView.mj_header.endRefreshing()
                
                SVProgressHUD.showError(withStatus: error?.domain)
                
                
            }
            
            
        }
    }






}







// MARK:- tableview代理方法
extension MyBankCarListVC{
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return 1
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if dataModels == nil {
            return 0
        }
        
        return dataModels?.count ?? 0
        
    }
    
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let tableCell : MyBankCarListCell =  tableView.dequeueReusableCell(withIdentifier: MyBankCarListCell.identifer()) as! MyBankCarListCell
        
        tableCell.model = dataModels?[indexPath.row]
        
        return tableCell
        
    }
    
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 82
    }
    
    
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //        super.tableView(tableView, didSelectRowAt: indexPath)
        
        
    }
    
    
    
}

